package com.cloud.controller;

import com.cloud.dto.ProjectDto;
import com.cloud.dto.TaskDto;
import com.cloud.entity.Project;
import com.cloud.entity.Task;
import com.cloud.service.ProjectService;
import com.cloud.service.TaskServiceI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("Task")
public class TaskController {
    @Autowired
    TaskServiceI taskService;

    @Autowired
    ProjectService projectService;

    //根据taskId找到task
    @GetMapping("/Tid/{id}")
    public Map<?,?> getTaskDtoByTId(@PathVariable("id") int taskId){
        Map<Object,Object> map = new HashMap<>();
        TaskDto taskByTid = taskService.getTaskByTid(taskId);
        map.put("task",taskByTid);
        return map;
    }

    //通过pid找到task
    @GetMapping("/Pid/{id}")
    public Map<?,?> getTaskDtoByPid(@PathVariable("id") int pid){
        Map<Object,Object> map = new HashMap<>();
        List<TaskDto> tasksByPid = taskService.findTasksBypId(pid);
        Project projectById = projectService.getProjectById(pid);
        map.put("compTaskNum",projectById.getCompTasknum());
        map.put("taskNum",projectById.getTasknum());
        map.put("task",tasksByPid);
        map.put("status",200);
        return map;
    }

    //展示未认领的列表
    @GetMapping("/ClamTaskList/{id}")
    public Map<?,?> ClaimTaskList(@PathVariable("id") int pid){
        Map<Object,Object> map = new HashMap<>();
        List<TaskDto> claimTaskList = taskService.ClaimTaskList(pid);
        map.put("claimTaskList",claimTaskList);
        return map;
    }

    //确认完成任务
    @RequestMapping("/finishTask/{t_id}/{u_id}")
    public Map<?,?> finishTask(@PathVariable("t_id") int t_id,@PathVariable("u_id") int u_id){
        HashMap<Object, Object> map = new HashMap<>();
        Task task = new Task();
        TaskDto taskByTid = taskService.getTaskByTid(t_id);
        taskByTid.setInfo("已完成");
        taskByTid.setTStatus(1);
        taskService.finishTask(taskByTid);
        List<TaskDto> MyTaskList=taskService.MyTaskList(u_id);
        List<TaskDto> MyTasks=taskService.TaskInfo(MyTaskList);
        map.put("MyTasks",MyTasks);
        return map;
    }

    //认领任务  领取任务之后，继续展示还没有被领取的任务
    @RequestMapping("/ClamTask/{t_id}/{user_id}/{p_id}")
    public Map<?,?> ClaimTask(@PathVariable("user_id")Integer user_id,@PathVariable("t_id") Integer t_id,@PathVariable("p_id") Integer p_id){
        Map<Object,Object> map = new HashMap<>();
        TaskDto taskDto=taskService.getTaskByTid(t_id);
        taskDto.setUserId(user_id);
        Task task = new Task();
        BeanUtils.copyProperties(taskDto,task);
        taskService.claimTask(taskDto);
        List<TaskDto> claimTaskList=taskService.ClaimTaskList(p_id);
        map.put("claimTaskList",claimTaskList);
        return map;
    }

    //查看我的任务列表
    @RequestMapping("/MyTaskList/{user_id}")
    public Map<Object,Object> MyTaskList(@PathVariable("user_id") Integer user_id){
        Map map = new HashMap();
        List<TaskDto> MyTaskList=taskService.MyTaskList(user_id);
        List<TaskDto> MyTasks=taskService.TaskInfo(MyTaskList);
        map.put("MyTasks",MyTasks);
        return map;
    }

    //前往修改task的内容，只是进入那个页面！！！
    @RequestMapping("/ToUpdateTask/{t_id}")
    public Map ToUpdateTask(@PathVariable("t_id") Integer t_id){
        Map map = new HashMap();
        TaskDto task=taskService.getTaskByTid(t_id);
        map.put("task",task);
        return map;
    }

    //请给我修改后的task的全部信息！！！
    @PostMapping("/updateTask")
    @CrossOrigin
    public Map UpdateTask(@RequestBody TaskDto taskDto){
        Map map = new HashMap();
        Task task = new Task();
        BeanUtils.copyProperties(taskDto,task);
        map.put("status",200);
        return map;
    }

    //添加任务  给我（Task 里面包含int user_id,int p_id,String title,String desc,String s_t,String e_t,int level(level=1为高优先级！！level=2中优先级  level=3低优先级  level=4无优先级)）这些东西
    @RequestMapping("/addTask")
    @CrossOrigin
    @ResponseBody
    public Map<?,?> addTask(@RequestBody Task task){
        Project curpro=projectService.getProjectById(task.getPId());
        int leader_id=curpro.getLeaderId();
        Map map = new HashMap();
        task.setLeaderId(leader_id);
        task.setUserId(0);
        task.setTStatus(0);
        task.setInfo("未完成");
        TaskDto taskDto = new TaskDto();
        BeanUtils.copyProperties(task,taskDto);
        TaskDto task1=taskService.addTask(taskDto);
//        System.out.println(task1);
//        if(task1!=null){
//            map.put("curpro",curpro);
//            List<TaskDto> tasks = taskService.findTask(task.getPId());
//            System.out.println("tasks"+tasks);
//            List<TaskDto> taskList = taskService.setTask(tasks);
//            System.out.println("tasksList"+taskList);
//            map.put("taskList",taskList);
//        }
        map.put("status",200);
        return map;
    }

    //前往添加任务页面
    @RequestMapping("/ToaddTask/{p_id}")
    public Map ToaddTask(@PathVariable("p_id") int p_id){
        HashMap<Object, Object> map = new HashMap<>();
        Project curpro=projectService.getProjectById(p_id);
        int leader_id=curpro.getLeaderId();
        int project_id=curpro.getPId();
        map.put("leader_id",leader_id);
        map.put("project_id",project_id);
        map.put("AddTask",1);
        return map;
    }

    @RequestMapping("/changeUser/{u_id}/{t_id}")
    public Map<?,?> changeUser(@PathVariable("u_id") Integer userId,@PathVariable("t_id") Integer t_id) {
        HashMap<Object, Object> map = new HashMap<>();
        TaskDto taskByTid = taskService.getTaskByTid(t_id);
        taskByTid.setUserId(userId);
        taskService.updateUserId(taskByTid);
        map.put("changeUser", taskByTid);
        return map;
    }

}
